<?php
namespace utils;

use think\facade\Db;

class Hcbgps {
    public static $api = 'https://open.figps.com/api/';
    public static $secrect = 'gu05yPuihcuw@4GE8V2z&Kza9RY3^tl8';
    public static $appid = '18609601202';
    public static $qq_map_key = 'UBIBZ-4RDKX-B3X4O-ZHA6W-3DXAZ-FVBLB';

    public static function getAccessToken() 
    {
        $methods = 'auth';
        $url = self::$api.$methods;
        $accessToken = '';
        $time = time();
        $token = Db::name('app_token')->where('appid', self::$appid)->find();
        if ($token) {
            $accessTokenRes = json_decode($token['access_token'], true);
            if (time() > $token['create_time'] + $accessTokenRes['expiresIn']/1000) {
                $data = [
                    'appid' => self::$appid,
                    'time' => $time,
                    'signature' => md5(md5(self::$secrect) . $time)
                ];
                $res = httpPost($url, $data);
                $res1 = json_decode($res, true);
                if ($res1['code'] != 0) {
                    throw new \Exception($res1['result'], 0);
                }
                Db::name('app_token')->where('appid', self::$appid)->update(['access_token' => $res, 'create_time' => time()]);
                $res = json_decode($res, true);
                $accessToken = $res['accessToken'];
            } else {
                $accessToken = $accessTokenRes['accessToken'];
            }
        } else {
            $data = [
                'appid' => self::$appid,
                'time' => $time,
                'signature' => md5(md5(self::$secrect) . $time)
            ];
            $res = httpPost($url, $data);
            $res1 = json_decode($res, true);
            if ($res1['code'] != 0) {
                throw new \Exception($res1['result'], 0);
            }
            Db::name('app_token')->insert(['appid' => self::$appid,'access_token' => $res, 'create_time' => time()]);
            $res = json_decode($res, true);
            $accessToken = $res['access_token'];
        }
        return $accessToken;
    }

    public static function getLocation($imei) 
    {
        try {
            $methods = 'device/location';
            $url = self::$api.$methods;
            $accessToken = self::getAccessToken();
            $data = [
                'imei' => $imei,
                'accessToken' => $accessToken,
            ];
            $param = ToUrlParams($data);
            $url .= '?'.$param;
            $res = httpGet($url);
            return json_decode($res, true);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }

    public static function getLocationHistory($imei, $start_time = '2023-03-11 13:40', $end_time='2023-03-11 13:45') 
    {
        try {
            $methods = 'device/track/history';
            $url = self::$api.$methods;
            $accessToken = self::getAccessToken();
            $data = [
                'imei' => $imei,
                'accessToken' => $accessToken,
                'startTime' => strtotime($start_time),
                'endTime' => strtotime($end_time),
                'coordType' => 'gcj02',
                'onlyGps' => '',
            ];
            $param = ToUrlParams($data);
            $url .= '?'.$param;
            $res = httpGet($url);
            return json_decode($res, true);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }

    //从其他坐标转换到qq坐标
    public static function translateToQq($type, $locations) 
    {
        $api = 'https://apis.map.qq.com/ws/coord/v1/translate';
        $data = [
            'key' => self::$qq_map_key,
            'locations' => $locations,
            'type' => $type,
        ];
        $param = ToUrlParams($data);
        $api .= '?'.$param;
        $res = httpGet($api);
        return json_decode($res, true);
    }
}